package org.example.server8001.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.example.SystemDemo.comment.res.*;
import org.example.SystemDemo.core.Result;
import org.example.SystemDemo.comment.enity.*;
import org.example.SystemDemo.comment.enity.ClientInfo;
import org.example.SystemDemo.comment.enity.HostInfo;
import org.example.server8001.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author xiebijiao
 * @version 1.0.0
 * @Description 将请求转发到客户端的控制层
 * @createTime 2021年11月16日 13:10
 */
@RestController
@RequestMapping("/forward")
public class ForwardController {

    @Autowired
    private InfoService infoService;

    private RestTemplate restTemplate = new RestTemplate();

    // 以下是获取动态信息

    /**
     * cpu信息
     * @param clientInfo
     * @return
     */
    @PostMapping("/dyCpuInfo")
    public Result<CPU> getDyCpuInfo(@RequestBody ClientInfo clientInfo) {
        return Result.ok(request("/DynamicInfo/getDynamicCPU",clientInfo,CPU.class));
    }

    /**
     * 网络信息
     * @param clientInfo
     * @return
     */
    @PostMapping("/dyNetInfo")
    public Result<ArrayList<Net>> getDyNetInfo(@RequestBody ClientInfo clientInfo) {
        return Result.ok(request("/DynamicInfo/getDynamicNet",clientInfo,ArrayList.class));
    }

    /**
     * 内存信息
     * @param clientInfo
     * @return
     */
    @PostMapping("/dyMemoryInfo")
    public Result<Memory> getDyMemoryInfo(@RequestBody ClientInfo clientInfo) {
        return Result.ok(request("/DynamicInfo/getDynamicMemory",clientInfo,Memory.class));
    }

    /**
     * 磁盘信息
     * @param clientInfo
     * @return
     */
    @PostMapping("/dyDiskInfo")
    public Result<ArrayList<Disk>> getDyDiskInfo(@RequestBody ClientInfo clientInfo) {
        return Result.ok(request("/DynamicInfo/getDynamicDisk",clientInfo,ArrayList.class));
    }


    //以下是获取静态信息

    /**
     * 系统版本
     * @param clientInfo
     * @return
     */
    @PostMapping("/sysVersionInfo")
    public Result<SystemVersionRESP> getSysVersionInfo(@RequestBody ClientInfo clientInfo) {
        return Result.ok(request("/StaticInfo/getSystemVersion",clientInfo,SystemVersionRESP.class));
    }

    /**
     * 系统内核
     * @param clientInfo
     * @return
     */
    @PostMapping("/sysCoreInfo")
    public Result<CoreInfoRESP> getSysCoreInfo(@RequestBody ClientInfo clientInfo) {
        return Result.ok(request("/StaticInfo/getCoreInfo",clientInfo,CoreInfoRESP.class));
    }

    /**
     * ip地址信息
     * @param clientInfo
     * @return
     */
    @PostMapping("/ipLocationInfo")
    public Result<List<IpLocationRESP>> getIpLocationInfo(@RequestBody ClientInfo clientInfo) {
        return Result.ok(request("/StaticInfo/getIpLocation",clientInfo,List.class));
    }

    /**
     * 内存信息
     * @param clientInfo
     * @return
     */
    @PostMapping("/memoryInfo")
    public Result<MemoryInfoRESP> getMemoryInfo(@RequestBody ClientInfo clientInfo) {
        return Result.ok(request("/StaticInfo/getMemoryInfo",clientInfo,MemoryInfoRESP.class));
    }

    /**
     * 磁盘信息
     * @param clientInfo
     * @return
     */
    @PostMapping("/diskInfo")
    public Result<List<DiskInfoRESP>> getDiskInfo(@RequestBody ClientInfo clientInfo) {
        return Result.ok(request("/StaticInfo/getDiskInfo",clientInfo,List.class));
    }


    // 获取客户端运行的端口号
    @Deprecated
    private String getRunPort(String ip) {
        QueryWrapper<HostInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("host_ip", ip);
        HostInfo hostInfo = infoService.getBaseMapper().selectOne(queryWrapper);
        return hostInfo.getRunPort();
    }

    private <T> T request(String path,ClientInfo clientInfo,Class<T> classT){
        verifyClient(clientInfo);
        String url = "http://" + clientInfo.getIp() + ":" + clientInfo.getPort() + path;
        return restTemplate.getForObject(url,classT);
    }

    private void verifyClient(ClientInfo clientInfo){
        if (!Objects.isNull(clientInfo.getId())){
            final HostInfo hostInfo = infoService.getBaseMapper().selectOne(Wrappers.<HostInfo>lambdaQuery().eq(HostInfo::getId, clientInfo.getId()));
            clientInfo.setIp(hostInfo.getHostIp());
            clientInfo.setPort(hostInfo.getRunPort());
        }else if (Objects.isNull(clientInfo.getPort())){
            clientInfo.setPort("8000");
        }
    }

}
